package org.example;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

public abstract class ImageState1 implements GameState {
    private final CodeRebirthGame game;
    private boolean showFirstImage = true;
    private Timer imageTimer;

    public ImageState1(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());
        game.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                showFirstImage = !showFirstImage;
                if (!showFirstImage) {
                    game.changeState(new ImageState2(game) {
                        @Override
                        public void render() {

                        }
                    });
                }
                game.repaint();
            }
        });

        imageTimer = new Timer();
        imageTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                showFirstImage = false;
                game.changeState(new ImageState2(game) {
                    @Override
                    public void render() {

                    }
                });
            }
        }, 10000);
    }

    @Override
    public void render(Graphics g) {
        g.setColor(showFirstImage ? Color.GREEN : Color.YELLOW);
        g.fillRect(0, 0, game.getWidth(), game.getHeight());
    }

    @Override public void update() {}
    @Override public void exit() {
        if (imageTimer != null) imageTimer.cancel();
        game.getContentPane().removeAll();
    }
    @Override public void handleInput(KeyEvent e) {}

    public abstract void render();
}